<?php
class heroModel extends appModel{
	public function isApply($wxid)
	{
		$sql = "SELECT count(*) FROM ".$this->table("hero_info")." WHERE wxid = $wxid LIMIT 1 ";
		return $this->getOne($sql); 
	}
	
	public function getApplyInfo($wxid)
	{
		$sql = "SELECT id,reason,pic_url FROM ".$this->table("run_apply")." WHERE wxid = $wxid LIMIT 1 ";
		$result = $this->getRow($sql);
		if(!$result)
		{
			return false;
		}
		else
		{
			$sql = "SELECT COUNT(*) FROM ".$this->table("run_vote")." WHERE rid=".$result['id'];
			$result['count'] = $this->getOne($sql);
			return $result;
		}
	}
	
	public function insertVote($wxid,$id)
	{
		$result = array("error"=>1,"content"=>"投票失败");
		if($wxid==$id){
			$result['content'] = "无法对自己投票";
			return $result;
		}
		$sql = "SELECT id FROM ".$this->table("run_apply")." WHERE wxid = $id LIMIT 1";
		$rid = $this->getOne($sql);
		$sql = "SELECT id FROM ".$this->table("run_vote")." WHERE wxid = $wxid AND rid = $rid LIMIT 1 ";
		$vid = $this->getOne($sql);
		if($vid)
		{
			$result['content'] = "您已对TA投票了，无法再次投票";
			return $result;
		}
		$info['wxid'] = $wxid;
		$info['rid'] = $rid;
		$res = $this->insert($info, "run_vote");
		if($res){
			$sql = "UPDATE ".$this->table("run_apply")." SET vote = vote+1 WHERE id =$rid ";
			$this->query($sql);
			$result['error'] = 0;
			$result['content'] = "投票成功";
		}
		return $result;
	}
}